<?php 
namespace app\admin\controller;
class Service extends Comm{
	public function service_list(){
		//面包屑标签显示
		$controller = request()->controller();
		$action = request()->action();
		$controller = model('Role')->where(['controller'=>$controller])->find()->rname;
		$action = model('Role')->where(['action'=>$action])->find()->rname;
		$this->assign('controller',$controller);
		$this->assign('action',$action);

		if(request()->ispost() && $_POST['cond'] != 'all'){
			$key = input('post.key');
			if(input('post.cond') == 'statu'){
				$tmp = ['待接单','待评价','已评价','订单取消'];
				$key = array_search($key,$tmp);
			}

			$where = [
				input('post.cond')=>$key
			];
			$re = model('Service')->alias('s')
							->join('Vip v','v.vid=s.vid')
							->join('Serviceini i','i.sid=s.sid')
							->join('Renter r','r.rid=s.rid')->where([input('post.cond')=>['like','%'.$key.'%'],'s.sid'=>['in','2,3']])->select();
			if($re == []){
				$data = [];
				$this->assign('data',$data);
				return $this->fetch();
			}
			foreach($re as $val){
				if($val['mid']>0){
					$uname = model('Member')->where(['uid'=>$val['mid']])->find()->uname;
					$val['uname'] = $uname;
				}
				$data[] = $val->toArray();
			}
			$this->assign('data',$data);
			return $this->fetch();
		}else{
			foreach(model('Service')->alias('s')
								->join('Vip v','v.vid=s.vid')
								->join('Serviceini i','i.sid=s.sid')
								->join('Renter r','r.rid=s.rid')->select() as $val){
				if($val['mid']>0){
					$uname = model('Member')->where(['uid'=>$val['mid']])->find()->uname;
					$val['uname'] = $uname;
				}
				$data[] = $val->toArray();
			}
			$this->assign('data',$data);
			return $this->fetch();
		}
	}

	//接单
	public function catch(){
		$id = $_POST['id'];
		$uname = session('uname');
		$sid = model('Service')->where(['id'=>$id])->find()->sid;
		$gname = model('Member')->where(['uname'=>session('uname')])->find()->gid;

		if($sid == 2 && $gname=='维修员'){
			$this->error('您没有权限接单');
		}elseif($sid == 3 && $gname=='保洁员'){
			$this->error('您没有权限接单');
		}
		
		$mid = model('Member')->where(['uname'=>$uname])->find()->uid;
		$res = model('Service')->where(['id'=>$id])->update(['statu'=>1,'mid'=>$mid]);
		if($res>0){
			$this->success('接单成功');
		}else{
			$this->error($res);
		}
		
	}

	//回评
	public function reply(){
		$id = $_POST['id'];
		$reply = $_POST['reply'];
		// dump($id);exit;
		$res = model('Service')->where(['id'=>$id])->update(['reply'=>$reply]);
		if($res>0){
			$this->success('回评成功');
		}else{
			$this->error($res);
		}
	}

	//评价显示
	public function look(){
		$id = $_POST['id'];
		
		$pdo = new \PDO('mysql:host=192.168.100.106;dbname=az','root','root');
		$pdo->exec('set names utf8');
		$sql = 'select * from service where id='.$id;
		$res = $pdo->query($sql)->fetchAll(2);
		$type = 1 - (int)$res[0]['type'];

		$res = model('Service')->where(['id'=>$id])->update(['type'=>$type]);
		if($res>0){
			$this->success('修改成功');
		}else{
			$this->error($res);
		}
	}

}



 ?>